﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BuyTicket.Models;
using System.Collections.ObjectModel;
using BuyTicket.Service;


namespace BuyTicket.Views
{
    /// <summary>
    /// ArgsView.xaml 的交互逻辑
    /// </summary>
    public partial class ArgsView : UserControl
    {
        public ArgsView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            List<Station> stations = Helper.ParseStation();
            cmbStart.ItemsSource = stations;
            cmbStart.DisplayMemberPath = "ChineseName";

            cmbEnd.ItemsSource = stations;
            cmbEnd.DisplayMemberPath = "ChineseName";

            cmbStart.SelectedIndex = cmbEnd.SelectedIndex = 0;
            listPerson.ItemsSource = new ObservableCollection<Person>(Global.MyTicketArgs.Persons);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text)
                || string.IsNullOrEmpty(txtPhone.Text)
                || string.IsNullOrEmpty(txtCard.Text))
            {
                MessageBox.Show("请填写完整的联系人信息！",
                    "错误提示", MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            else
            {
                var querys = Global.MyTicketArgs.Persons
                    .Where(m => m.Card.Equals(txtCard.Text));
                if (querys.Count() > 0)
                {
                    MessageBox.Show("该联系人已存在！",
                    "错误提示", MessageBoxButton.OK,
                    MessageBoxImage.Error);
                }
                else
                {
                    if (Global.MyTicketArgs.Persons.Count >= 5)
                    {
                        MessageBox.Show("最多只能添加3位用户！",
                   "错误提示", MessageBoxButton.OK,
                   MessageBoxImage.Error);
                        return;
                    }
                    Global.MyTicketArgs.Persons.Add(new Person(
                        txtName.Text,
                        txtPhone.Text,
                        txtCard.Text));
                    listPerson.ItemsSource = new ObservableCollection<Person>(Global.MyTicketArgs.Persons);

                    txtName.Text = txtPhone.Text = txtCard.Text = "";
                }
            }
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (!dateTime.SelectedDate.HasValue)
            {
                MessageBox.Show("请先选择乘车日期！",
                  "错误提示", MessageBoxButton.OK,
                  MessageBoxImage.Error);
                return;
            }
            Global.MyTicketArgs.StartStation = cmbStart.SelectedValue as Station;
            Global.MyTicketArgs.EndStation = cmbEnd.SelectedValue as Station;
            Global.MyTicketArgs.Date = dateTime.SelectedDate.Value;
            Global.MyTicketArgs.IsStudent = ckStudent.IsChecked.Value;
            if (Global.MyTicketArgs.Verify())
            {
                StringBuilder bulider = new StringBuilder();
                bulider.AppendLine("请确认以下购票信息：");
                bulider.AppendLine("------------------------------");
                bulider.AppendLine("");
                bulider.AppendLine(string.Format("起始站：{0}   终点站：{1}",
                    Global.MyTicketArgs.StartStation.ChineseName,
                    Global.MyTicketArgs.EndStation.ChineseName));
                bulider.AppendLine(string.Format("乘车日期：{0}",
                    Global.MyTicketArgs.Date.ToShortDateString()));
                bulider.AppendLine("");
                bulider.AppendLine("------------------------------");
                bulider.AppendLine("姓名\t电话\t证件");
                foreach (var person in Global.MyTicketArgs.Persons)
                {
                    bulider.AppendLine(string.Format("{0}\t{1}\t{2}",
                        person.Name,
                        person.Phone,
                        person.Card));
                }
                var result = MessageBox.Show(bulider.ToString(), "信息确认",
                    MessageBoxButton.OKCancel,
                    MessageBoxImage.Question);
                if (result == MessageBoxResult.OK)
                {
                    NavigationService.GetNavigationService(this)
                        .Navigate(new Uri("Views/BoardView.xaml", UriKind.Relative));
                }
            }
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            BuyTicketService.Navigate("https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init");
        }
    }
}
